package com.example.demo4.dao;

import com.example.demo4.Bean.Admin;
import com.example.demo4.utils.DBConnection;
import com.example.demo4.utils.Time;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDao {
    DBConnection DBConn = new DBConnection();
    /**
     * 方法名:CheckLogin
     * 功能描述:登录验证
     * Created by 天马行空
     */
public boolean CheckLogin( String s1, String s2) throws SQLException {
    Connection conn = DBConn.getConn();
    Statement stmt = conn.createStatement();
    ResultSet rs = null ;
    boolean OK = true;
    String sql = "select * from admin where adminname = '"+s1+"'and" + " adminpwd = '"+s2+"'";
    rs = stmt.executeQuery(sql);
    if(!rs.next()){
        OK = false;
    }
    return OK;
    }
    DBConnection DBCoon = new DBConnection();
    public List<Admin> getList(int page) {
        Connection conn = DBCoon.getConn();
        String sql = "select * from admin limit "+(page-1)*10+",10";
        List<Admin> list = new ArrayList<Admin>();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Admin adminBean = new Admin();
                adminBean.setAdminID(rs.getInt(1));
                adminBean.setAdminName(rs.getString(2));
                adminBean.setAdminpwd(rs.getString(3));
                adminBean.setAdmintype(rs.getString(5));
                adminBean.setLastLogintime(Time.getDateTime());
                list.add(adminBean);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }


    public int updateNews(Admin admin) {
        if (admin.getAdminID() > 0) {
            Connection conn = DBCoon.getConn();
            try {
                String sql = "update admin set adminname=?,adminpwd=?,admintype=?,lastlogintime=? where adminid=?";
                PreparedStatement pmst = conn.prepareStatement(sql);
                pmst.setString(1, admin.getAdminName());
                pmst.setString(2, admin.getAdminpwd());
                pmst.setString(3, admin.getAdmintype());
                pmst.setString(4, Time.getDateTime());
                pmst.setInt(5, admin.getAdminID());
                return pmst.executeUpdate();
            } catch (SQLException e){
                e.printStackTrace();
            }
            return 0;
        }
        return 0;
    }

    public int add(Admin admin){
        Connection conn = DBCoon.getConn();
        try {
            String sql = "insert into admin values(null,?,?,?,?)";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, admin.getAdminName());
            pmst.setString(2, admin.getAdminpwd());
            pmst.setInt(3, 0);
            pmst.setString(4,Time.getDateTime());
            System.out.println(pmst);
            return pmst.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }
    public int deleteNewsById(int newsid){
        Connection conn = DBCoon.getConn();
        try {
            String sql = "delete from admin where adminID=?";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1,newsid);
            return pmst.executeUpdate();
        }catch (SQLException e){
            System.out.println("deleteNewsById error");
            e.printStackTrace();
        }
        return 0;
    }

    public int countPage() {
        Connection conn = DBCoon.getConn();
        String sql = "select count(*) from admin";
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);//获取查询结果
            if (rs.next()){
                return (rs.getInt(1)/10)+1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
}
